package com.pubinfo.passbook.common.service.master.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TRuleCity;
import com.pubinfo.passbook.common.entity.TRuleGenus;
import com.pubinfo.passbook.common.mapper.TRuleCityMapper;
import com.pubinfo.passbook.common.model.dto.rule.RuleGenusParam;
import com.pubinfo.passbook.common.service.master.TRuleGenusService;
import com.pubinfo.passbook.common.mapper.TRuleGenusMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Service
@Slf4j
public class TRuleGenusServiceImpl extends ServiceImpl<TRuleGenusMapper, TRuleGenus>
    implements TRuleGenusService {
  
  @Resource
  private TRuleCityMapper cityMapper;
  
  @Override
  public boolean saveRuleGenus(TRuleGenus ruleGenus) {
    TRuleCity tRuleCity = cityMapper.selectById(ruleGenus.getCityId());
    if (tRuleCity != null) {
      return this.baseMapper.insert(ruleGenus) > 0;
    }
    return false;
  }
  
  @Override
  public boolean deleteRuleGenus(String id) {
    return this.baseMapper.deleteRoleGenus(id);
  }
  
  @Override
  public Boolean isGenus(String idNo) {
    QueryWrapper<TRuleGenus> ruleGenusQueryWrapper = new QueryWrapper<>();
    ruleGenusQueryWrapper.eq("id_no", idNo);
    List<TRuleGenus> ruleGenus = list(ruleGenusQueryWrapper);
    return !CollectionUtils.isEmpty(ruleGenus);
  }
  
  @Override
  public String findAcctByidNo(String idNo) {
    
    return findRuleGenusByidNo(idNo).getAcct();
  }
  
  @Override
  public TRuleGenus findRuleGenusByidNo(String idNo) {
    QueryWrapper<TRuleGenus> ruleGenusQueryWrapper = new QueryWrapper<>();
    ruleGenusQueryWrapper.eq("id_no", idNo);//供属身份证号
    List<TRuleGenus> tRuleGenusList = list(ruleGenusQueryWrapper);
    if (CollectionUtils.isEmpty(tRuleGenusList)) {
      return null;
    }
    TRuleGenus ruleGenus = tRuleGenusList.get(0);
    return ruleGenus;
  }
  
  @Override
  public IPage<TRuleGenus> selectRuleGenus(RuleGenusParam param) {
    IPage<TRuleGenus> iPage = page(param.getPage("create_time"));
    return this.baseMapper.selectRuleGenus(iPage, param);
  }
}




